package com.xuecheng.content.controller;

import com.xuecheng.api.content.model.api.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.po.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;

import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CoursePubService;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;

    @Autowired
    private CoursePubService coursePubService;
    /**
     * 测试数据库是否正常
     */
    //
    @PostMapping("/course/list")
    public PageVO<CourseBaseDTO> queryCourseList( PageRequestParams params, @RequestBody QueryCourseModel model){
        Long companyId = UAASecurityUtil.getCompanyId();
        return courseBaseService.queryCourseList(params,model,companyId);
    }
    // 增
    @PostMapping("course")
    public CourseBaseDTO createCourse(@RequestBody CourseBaseVO courseBaseVO) {
        //courseBaseVO 转化为dto
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.createCourse(courseBaseDTO);
    }
    // 改
    @PutMapping("/course")
    public CourseBaseDTO modifyCourse(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.modifyCourse(courseBaseDTO,companyId);
    }
    //删
    @DeleteMapping("course/{courseId}")
    public void removeCourseBaseById(@PathVariable Long courseId) {

        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseService.removeCourseBase(courseId, companyId);

    }

    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseById(@PathVariable Long courseBaseId) {

        Long companyId = UAASecurityUtil.getCompanyId();

        return courseBaseService.getCourseById(courseBaseId,companyId);
    }

    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {

        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseService.commitCourseBase(courseBaseId,companyId);
    }

    @PostMapping("course_pub/publish/{courseBaseId}")
    public void publish(@PathVariable Long courseBaseId){

        Long companyId = UAASecurityUtil.getCompanyId();

        coursePubService.publish(courseBaseId,companyId,false);

    }


}
